commonlibsse_ng\re\n/
NiMemManager.rs

1use core::ffi::{c_char, c_void};
2use core::ptr::{self, NonNull};
3
4use crate::re::NiAllocator::{NiAllocator, NiMemEventType};
5
6#[derive(Debug)]
7#[repr(C)]
8pub struct NiMemManager {
9    pub allocator: Option<NonNull<NiAllocator>>,
10}
11const _: () = assert!(core::mem::size_of::<NiMemManager>() == 0x8);
12
13#[allow(clippy::too_many_arguments)]
14impl NiMemManager {
15    /// Returns the singleton instance of `Self`.
16    #[commonlibsse_ng_derive_internal::relocate(
17        cast_as = "*mut *mut NiMemManager",
18        default = "None",
19        deref_once,
20        id(se = 523759, ae = 410319)
21    )]
22    pub fn get_singleton() -> Option<&'static NiMemManager> {
23        |deref_type: DerefType| unsafe { deref_type.as_ref() }
24    }
25
26    /// Returns the singleton instance of `Self`.
27    #[commonlibsse_ng_derive_internal::relocate(
28        cast_as = "*mut *mut NiMemManager",
29        deref_once,
30        default = "None",
31        id(se = 523759, ae = 410319)
32    )]
33    pub fn get_singleton_mut() -> Option<&'static mut NiMemManager> {
34        |deref_type: DerefType| unsafe { deref_type.as_mut() }
35    }
36
37    pub fn allocate(
38        &mut self,
39        size_in_bytes: usize,
40        alignment: usize,
41        event_type: NiMemEventType,
42        provide_accurate_size_on_deallocate: bool,
43        file: Option<*const c_char>,
44        line: Option<i32>,
45        function: Option<*const c_char>,
46    ) -> Option<NonNull<u8>> {
47        let allocator = unsafe { self.allocator?.as_mut() };
48        let mut size_in_bytes = size_in_bytes;
49        let mut alignment = alignment;
50
51        let file = file.unwrap_or(ptr::null_mut());
52        let line = line.unwrap_or(-1);
53        let function = function.unwrap_or(ptr::null_mut());
54
55        let mem_ptr = (allocator.vtable().Allocate)(
56            allocator,
57            &mut size_in_bytes,
58            &mut alignment,
59            event_type,
60            provide_accurate_size_on_deallocate,
61            file,
62            line,
63            function,
64        )
65        .cast();
66
67        NonNull::new(mem_ptr)
68    }
69
70    pub fn deallocate(
71        &mut self,
72        memory: *mut c_void,
73        event_type: NiMemEventType,
74        size_in_bytes: Option<usize>,
75    ) {
76        let size_in_bytes = size_in_bytes.unwrap_or(usize::MAX);
77
78        if let Some(allocator) = self.allocator.as_mut() {
79            let deallocate = unsafe { allocator.as_mut().vtable().Deallocate };
80            (deallocate)(allocator.as_ptr(), memory, event_type, size_in_bytes);
81        };
82    }
83
84    pub fn reallocate(
85        &mut self,
86        memory: *mut c_void,
87        size_in_bytes: *mut usize,
88        alignment: *mut usize,
89        event_type: NiMemEventType,
90        provide_accurate_size_on_deallocate: bool,
91        size_current: usize,
92        file: *const c_char,
93        line: i32,
94        function: *const c_char,
95    ) -> Option<NonNull<u8>> {
96        let allocator = unsafe { self.allocator?.as_mut() };
97        let mem_ptr = (allocator.vtable().Reallocate)(
98            allocator,
99            memory,
100            size_in_bytes,
101            alignment,
102            event_type,
103            provide_accurate_size_on_deallocate,
104            size_current,
105            file,
106            line,
107            function,
108        )
109        .cast();
110
111        NonNull::new(mem_ptr)
112    }
113
114    pub fn track_allocate(
115        &mut self,
116        memory: *const c_void,
117        size_in_bytes: usize,
118        event_type: NiMemEventType,
119        file: *const c_char,
120        line: i32,
121        function: *const c_char,
122    ) -> bool {
123        let allocator = match self.allocator {
124            Some(mut allocator) => unsafe { allocator.as_mut() },
125            None => return false,
126        };
127
128        (allocator.vtable().TrackAllocate)(
129            allocator,
130            memory,
131            size_in_bytes,
132            event_type,
133            file,
134            line,
135            function,
136        )
137    }
138
139    pub fn track_deallocate(&mut self, memory: *const c_void, event_type: NiMemEventType) -> bool {
140        let allocator = match self.allocator {
141            Some(mut allocator) => unsafe { allocator.as_mut() },
142            None => return false,
143        };
144
145        (allocator.vtable().TrackDeallocate)(allocator, memory, event_type)
146    }
147}